package com.hqd.ch03.v31.web.servlet.handler;


import com.hqd.ch03.v31.web.method.HandlerMethod;
import com.hqd.ch03.v31.web.servlet.HandlerMapping;
import com.hqd.ch03.v31.web.utils.UrlPathHelper;

import javax.servlet.http.HttpServletRequest;


public abstract class AbstractHandlerMapping implements HandlerMapping {
    /**
     * 默认处理器
     */
    private Object defaultHandler;
    private UrlPathHelper urlPathHelper = new UrlPathHelper();


    @Override
    public final HandlerMethod getHandler(HttpServletRequest request) throws Exception {
        //具体获取方式由子类实现
        Object handler = getHandlerInternal(request);
        if (handler == null) {
            handler = getDefaultHandler();
        }
        return (HandlerMethod) handler;
    }

    protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception;

    public UrlPathHelper getUrlPathHelper() {
        return urlPathHelper;
    }


    public Object getDefaultHandler() {
        return this.defaultHandler;
    }

    public void setDefaultHandler(Object defaultHandler) {
        this.defaultHandler = defaultHandler;
    }
}
